package me.sluijsens.AntiEnderman;

import java.util.logging.Logger;

import net.milkbowl.vault.permission.Permission;

import org.bukkit.ChatColor;
import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;

public class AntiEnderman extends JavaPlugin {
	private final EntityListener entityListener = new EntityListener(this);
	Logger log = Logger.getLogger("Minecraft");
	public static String tagChat;

	private ConfigHandler conf = null;

	public boolean in_Array(String[] array, String value) {
		int length = array.length;

		for (int i = 0; i < length; i++) {
			if (array[i].equalsIgnoreCase(value)) {
				return true;
			}
		}

		return false;
	}

	public static Permission permission = null;

	private Boolean setupPermissions() {
		RegisteredServiceProvider<Permission> permissionProvider = getServer()
				.getServicesManager().getRegistration(
						net.milkbowl.vault.permission.Permission.class);
		if (permissionProvider != null) {
			permission = permissionProvider.getProvider();
		}
		return (permission != null);
	}

	public boolean in_Array(Object[] array, Object value) {
		int length = array.length;

		for (int i = 0; i < length; i++) {
			if (array[i] == value) {
				return true;
			}
		}

		return false;
	}

	public void onEnable() {
		if (getServer().getPluginManager().getPlugin("Vault") != null)
			setupPermissions();
		tagChat = ChatColor.WHITE + "~" + ChatColor.RED + " Anti"
				+ ChatColor.WHITE + "-" + ChatColor.BLUE + "Enderman "
				+ ChatColor.WHITE + "~ ";

		// Create config file
		setConf(new ConfigHandler("config", this.getDataFolder().toString(),
				this));

		if (!conf.isSet("block.enderman.spawn")) {
			conf.set("block.enderman.spawn", true);
			conf.set("block.enderman.place", true);
			conf.set("block.enderman.break", true);
			conf.set("block.enderman.teleport", true);

			conf.save();
		}

		// Handle events
		PluginManager pm = this.getServer().getPluginManager();
		pm.registerEvents(entityListener, this);

		cmdExecutor cmdExecutor = new cmdExecutor(this);
		getCommand("ae").setExecutor(cmdExecutor);
	}

	public void onDisable() {
		
	}

	public ConfigHandler getConf() {
		return conf;
	}

	public void setConf(ConfigHandler conf) {
		this.conf = conf;
	}
}
